package com.ruoyi.project.system.task.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.post.domain.Post;
import com.ruoyi.project.system.post.mapper.PostMapper;
import com.ruoyi.project.system.task.domain.Transfer;
import com.ruoyi.project.system.task.mapper.TransferMapper;
import com.ruoyi.project.system.task.service.TransferService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/system/task/replace")
public class ReplaceController extends BaseController {

    private String prefix = "system/task/replace";

    @Autowired
    private TransferService transferService;

    @Autowired
    private TransferMapper transferMapper;

    @Autowired
    private PostMapper postMapper;

    /**
     * 跳转到任务转移
     * @return
     */
    @RequiresPermissions("system:replace:view")
    @GetMapping()
    public String task()
    {
        return prefix + "/replace";
    }

    /**
     * 审核任务转移
     */
    @GetMapping("/edit/{transferId}")
    public String edit(@PathVariable("transferId") Integer transferId, ModelMap mmap)
    {   //根据转移任务id查看任务信息
        Transfer transfer = transferService.selectTranfById(transferId);
        if(transfer.getSuccessor() != null){
            //根据查到的继任者id查询继任者姓名
            Transfer transfer1 = transferMapper.selectTranfBySuccessor(transfer.getSuccessor());
            //把查询到的继任者姓名存入对象
            transfer.setSucceName(transfer1.getSucceName());
        }
        mmap.put("replace", transfer);
        return prefix + "/edit";
    }

    /**
     * 审核任务转移
     */
    @RequiresPermissions("system:replace:edit")
    @Log(title = "任务转移", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Transfer transfer)
    {
        //生成审核时间并注入对象
        transfer.setAuditTime(new Date());
        System.out.println(transfer.toString());
        Long userId = ShiroUtils.getUserId();
        //查询当前用户的职位并注入对象
        List<Post> posts = postMapper.selectPostsByUserId(userId);
        transfer.setAuditor(posts.get(0).getPostName());
        return toAjax(transferService.updateTranfTask(transfer));
    }

    /**
     * 更具条件分页查询
     * @param transfer 条件
     * @return
     */
    @RequiresPermissions("system:replace:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Transfer transfer)
    {
        startPage();
        List<Transfer> list = transferService.selectTransferList(transfer);
        return getDataTable(list);
    }

    /**
     * 一键导出表
     * @param transfer 查询条件
     * @return
     */
    @Log(title = "任务转移审核", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:replace:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Transfer transfer)
    {
        List<Transfer> list = transferService.selectTransferList(transfer);
        ExcelUtil<Transfer> util = new ExcelUtil<Transfer>(Transfer.class);
        return util.exportExcel(list, "任务转移数据");
    }

    /**
     * 根据转移任务id查看任务信息
     * @param transferId 延期id
     * @param mmap
     * @return
     */
    @GetMapping("/show/{transferId}")
    public String show(@PathVariable("transferId") Integer transferId, ModelMap mmap)
    {   //根据转移任务id查看任务信息
        Transfer transfer = transferService.selectTranfById(transferId);
        if(transfer.getSuccessor() != null){
            //根据查到的继任者id查询继任者姓名
            Transfer transfer1 = transferMapper.selectTranfBySuccessor(transfer.getSuccessor());
            //把查询到的继任者姓名存入对象
            transfer.setSucceName(transfer1.getSucceName());
        }
        mmap.put("replace", transfer);
        return prefix + "/show";
    }
    
}
